package com.example.a2302fx.register

import android.os.Bundle
import android.text.Editable
import android.text.SpannableStringBuilder
import android.text.TextWatcher
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.view.View
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.RegexUtils
import com.blankj.utilcode.util.ToastUtils
import com.example.a2302fx.R
import com.example.a2302fx.databinding.ActivityRegisterBinding
import com.example.base.mvvm.BaseActivity
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

@Route(path = "/app/RegisterActivity")
class RegisterActivity : BaseActivity<ActivityRegisterBinding,RegisterViewModel>() {
    override fun initData() {
        viewModel.codeSuccess.observe(this){
           if(it.code==200){
               binding.etCode.setText(it.data.content.substring(8,14))
           }
        }
        viewModel.regSuccess.observe(this){
            if(it.code==200){
                finish()
            }else{
                ToastUtils.showLong(it.message)
            }
        }
    }

    override fun initView() {
        //先监听手机号是否输入了11位
        binding.etUsername.addTextChangedListener(object :TextWatcher{
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
              if(s!=null){
                  if(s.length==11){
                      binding.btnCode.isEnabled = true
                  }else{
                      binding.btnCode.isEnabled = false
                  }
              }
            }

            override fun afterTextChanged(s: Editable?) {

            }
        })
        //发送验证码
       binding.btnCode.setOnClickListener {
           if(!RegexUtils.isMobileExact(binding.etUsername.text.toString())){
               ToastUtils.showLong("手机号格式不正确，请重新输入")
               return@setOnClickListener
           }
           viewModel.code(binding.etUsername.text.toString())
           //开倒计时
           binding.btnCode.isEnabled = false
           lifecycleScope.launch {
               for (i in 10 downTo 1){
                   binding.btnCode.text = "$i 秒后重新获取"
                   delay(1000)
                   if(i==1){
                       binding.btnCode.isEnabled = true
                   }
               }

           }
       }
        //点击登录
        binding.register.setOnClickListener {
            //校验写全
            val username = binding.etUsername.text.toString()
            val etCode = binding.etCode.text.toString()
            val password = binding.etPassword.text.toString()
            val rePassword = binding.etRepassword.text.toString()
            val email = binding.etEmail.text.toString()
            if(username.isEmpty()||etCode.isEmpty()||password.isEmpty()||rePassword.isEmpty()||email.isEmpty()){
                ToastUtils.showLong("所有输入框均为必填项")
                return@setOnClickListener
            }
            if(!RegexUtils.isMobileExact(username)){
               ToastUtils.showLong("手机号格式不正确")
                return@setOnClickListener
            }
            if(password!=rePassword){
                ToastUtils.showLong("两次密码不一致")
                return@setOnClickListener
            }
            if(!RegexUtils.isEmail(email)){
                ToastUtils.showLong("邮箱格式不正确")
                return@setOnClickListener
            }
            if(!binding.cb.isChecked){
                ToastUtils.showLong("请先勾选同意协议")
                return@setOnClickListener
            }
            viewModel.register(etCode,username)
        }
        //富文本
        val spannableStringBuilder = SpannableStringBuilder(binding.cb.text.toString())
        val clickableSpan = object:ClickableSpan(){
            override fun onClick(widget: View) {
                ARouter.getInstance().build("/app/WebViewActivity").withString("url","http://www.baidu.com").navigation()
            }
        }
        spannableStringBuilder.setSpan(clickableSpan,6,13,SpannableStringBuilder.SPAN_INCLUSIVE_INCLUSIVE)
        binding.cb.movementMethod = LinkMovementMethod.getInstance()
        binding.cb.text = spannableStringBuilder

    }

    override fun getViewModel(): Class<RegisterViewModel> = RegisterViewModel::class.java

    override fun getLayout(): Int = R.layout.activity_register

}